<?php
/**
*
* @package phpBB3
* @version $Id: users_add.php,v 1.0.0 2008/07/29 00:00:00 Mighty Gorgon Exp $
* @copyright (c) 2008 Mighty Gorgon
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/

@set_time_limit(0);
//@ignore_user_abort(true);
@ini_set('memory_limit', '64M');

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

$users_start = request_var('us', 0);

$users_data = array();

if (isset($_POST['list'])) {
   $list = explode("\n", $_POST['list']);
   $users_counter = 0;
   foreach ($list as $key => $row) {
      $data = explode(",", $row);
      $data = str_replace("\"", "", $data);
      $data = str_replace("\\", "", $data);
      $num = count($data);
      for ($c = 0; $c < $num; $c++)
      {
         $users_data[$users_counter][$c] = $data[$c];
      }
      $users_counter++;
   }
}else{
   die("ERROR!!");
}

$users_list = '';
$total_users = count($users_data);
$new_start = $users_start;

$users_fields_name = array('username', 'user_password', 'user_email', 'group_id', 'user_timezone', 'user_lang', 'user_type', 'user_regdate');
for ($i = $users_start; $i < $total_users; $i++)
{
   $users_fields_values = array($users_data[$i][0], phpbb_hash($users_data[$i][1]), $users_data[$i][2], $users_data[$i][3], 7.00, 'en', '0', time());
   for ($j = 0; $j < count($users_fields_name); $j++)
   {
      $users_data[$i][$j] = (empty($users_data[$i][$j])) ? $users_fields_values[$j] : $users_data[$i][$j];
      $users_data[$i][$users_fields_name[$j]] = $users_fields_values[$j];
      unset($users_data[$i][$j]);
   }
   $user_id = user_add($users_data[$i]);
   if ($user_id !== false)
   {
      $users_list .= (($users_list == '') ? '' : ', ') . $users_data[$i]['username'];
   }
   $new_start++;
}

$message_text = 'The following users have been added to the DB:<br /><br />' . $users_list;

if ($new_start >= $total_users)
{
   $message = 'All the users have been imported correctly!<br /><br />' . $message_text;
   $template->assign_vars(array(
      'MESSAGE_TITLE' => 'ADDING USERS',
      'MESSAGE_TEXT' => $message
      )
   );
   page_header('ADDING USERS');
   $template->set_filenames(array('body' => 'message_body.html'));
   page_footer();
}
else
{
   $meta_url = append_sid("{$phpbb_root_path}users_add.$phpEx", "us=$new_start&amp;up=$users_step");
   meta_refresh(3, $meta_url);
   $message = 'The process is not finished yet, this page will redirect automatically in few seconds, please wait...<br /><br />' . $message_text;
   trigger_error($message);
   exit;
}

?>
